<?php

namespace App\Console\Commands\User;

use App\Repositories\User\UserRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

/**
 * Class CheckVipIsExpire
 * @package App\Console\Commands\User
 */
class CheckVipIsExpire extends Command
{

    protected $signature = 'user:vip:checkExpire';

    protected $description = 'check user is expire';

    private $userRepository;

    private $logger;

    /**
     * VipExpireRemind constructor.
     * @param UserRepository $userRepository
     */
    public function __construct(UserRepository $userRepository)
    {
        parent::__construct();

        $this->userRepository = $userRepository;

        $this->logger = Log::channel('console');
    }

    /**
     * @throws \Exception
     */
    public function handle()
    {
        
        foreach ($this->userRepository->getExpireVipUser() as $uid) {
            
            try {
                
                $this->userRepository->resetUserVip($uid);
                
            } catch (\Exception $e) {
                
                $this->logger->error($this->signature, [$e]);

                $this->error($e->getMessage());
            }
        }

        $this->info($this->signature . ' success！'. (new Carbon())->toDateTimeString());
    }
}